package com.haipumi.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.haipumi.beans.haipumi_reply;
import com.haipumi.beans.haipumi_task;
import com.haipumi.dao.DBHelper;
import com.haipumi.dao.IBackTaskDao;

public class BackTaskDaoImpl implements IBackTaskDao {

	DBHelper db=new DBHelper();
	
	@Override
	public List<haipumi_task> findAllTask() {
		String sql="select * from haipumi_task ";
		List<haipumi_task> tasklist=null;
		try {
			tasklist=db.finds(sql, null, haipumi_task.class);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tasklist;
	}

	@Override
	public int countAllTask() {
		String sql="select count(*) from haipumi_task ";
		int r=0;
		try {
			r=(int) db.findDouble(sql, null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return r;
	}

	@Override
	public haipumi_task checkTask(String uid) {
		String sql=" update haipumi_task set tstatus=1 where tid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(uid);
		int result=0;
		haipumi_task backtask=null;
		try {
			result=db.doUpdate(sql, params);
			if(result>0){
				backtask=findTask(uid);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return backtask;
	}
	
	public haipumi_task findTask(String uid){
		String sql=" select * from haipumi_task where tid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(uid);
		haipumi_task task=null;
		List<haipumi_task>tasklist=null;
		try {
			tasklist=db.finds(sql, params, haipumi_task.class);
			if(!tasklist.isEmpty()){
				task=tasklist.get(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return task;
	}

	@Override
	public boolean frazetask(String tid) {
		String sql=" update haipumi_task set tstatus=4 where tid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(tid);
		int result=0;
		try {
			result=db.doUpdate(sql, params);
			if(result>0){
				return true;	
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public boolean timeouttask(String tid) {
		String sql=" update haipumi_task set tstatus=5 where tid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(tid);
		int result=0;
		try {
			result=db.doUpdate(sql, params);
			if(result>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public boolean finishTask(String tid) {
		String sql=" update haipumi_task set tstatus=3 where tid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(tid);
		int result=0;
		try {
			result=db.doUpdate(sql, params);
			if(result>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	
}
